*Treatment data:
*Follow the definition of the variables and Sections III and IV of the paper.
*Merge municipality-of-birth distances to the closest MW for each birthyear 1931-1946. This file also includes gravity measures.
*Create a DD indicator postXMW when this distance turns to be less or equal to 5.5 km.
*Create cohortXmunicipality specific event-years for a DD indicator: pre(i)XMW and post(i)XMW 
*Merge information on MWs' municipalities that registered births instead of real municipalities of birth (in respective years), and mark them together with municipalities within 5.5. radius as "wrongregister".
*Based on the variation of postXMW within bmunicipality, create several groups of municipalities, such as: treated=1 (those that change the treatment status in any year in 1931-1946); nevertreated=1 (those that never become treated in 1931-1946); alwaystreated (treated before 1931) [technically speaking, those treated in 1931 are also always-treated]
*firstyearofMW = the first year since the bmunicipality has access to MW within 5.5 km radius
mws.dta, replace

*Cointerventions' treatment data:
*Follow the definition of the variables and Section V.C. of the paper.
*For the definitions of the variables used in Appendix only, follow Appendix J (pronstosil, wwii, schoolingreform)
*Create a DD indicator cointervention for sulpha antibiotics.
*Create cohortXmunicipality specific event-years for a DD indicator: pre(i)Xcointervention and post(i)Xcointervention 
sulpha.dta, replace
*Create a DD indicator cointervention for well-child program.
*Create cohortXmunicipality specific event-years for a DD indicator: pre(i)Xcointervention and post(i)Xcointervention 
wellchild.dta, replace
*Create a DD indicator cointervention for pronstosil, wwii, and schoolingreform.
prontosil.dta, replace
wwii.dta, replace
schoolingreform.dta, replace

*Outcome samples
*Create samples of data for all outcomes, following the definition of the variables and Section IV of the paper.
*In each of these samples create a variable called "mainoutcome" equal to the outcome.
*Each file should include an ID variable that will be linked to the Treatment file, and individual-level information such sex, year of birth (byear) and the regionXurban indicator.
*Save all samples, they include:
hospitalshare.dta
earlymortality.dta
latemortality.dta
neonatalmortality.dta
lnlabourincome.dta
disabled.dta
unemployed.dta
nonmanual.dta
service.dta
yearsofschooling.dta
secondaryschool.dta
specificfield.dta
totalhospitalstay.dta
premature.dta
fulltermstillbirths.dta
stillbirths.dta
sickmothers.dta
infectious.dta
cardiovascular.dta
diabetes.dta
cancer.dta
degenerative.dta
mental.dta
postmortality.dta
childmortality.dta
adultmortality1536.dta
adultmortality3764.dta
infectiousmort.dta
cardiovascularmort.dta
diabetesmort.dta
cancermort.dta
degenerativemort.dta
mentalmort.dta
othermort.dta
socialsciences.dta
naturalsciences.dta
medicine.dta
otherfields.dta
agriculture.dta
industry.dta
building.dta
transport.dta
publicservices.dta
private.dta
government.dta
publicadmin.dta
teaching.dta
lndoctors.dta
lnmidwives.dta
lnnurses.dta
lnhospitals.dta
lnexpenditures.dta
lbirths.dta
children.dta
migrant.dta
*Save other region-urban controls into
othercontrols.dta
*Put parental controls and motherid into a separate dataset
parental.dta
*Place individual-years (one row per individual-year) into a separate dataset (these are all observed dates, starting from Census 1960)
iddates.dta



*Table 1
foreach i in hospitalshare earlymortality latemortality neonatalmortality {
	use `i'.dta, replace
	merge 1:1 bmunicipality byear using mws.dta
	xtset bmunicipality
	xtreg mainoutcome postXMW i.byear i.byear#i.regionXurban if wrongregister==0, fe vce(cluster bmunicipality)
	outreg2 using `i', cttop(coefficient) word label se e(all) dec(3)
}

*Figure 2
foreach i in hospitalshare earlymortality latemortality neonatalmortality {
	use `i'.dta, replace
	merge 1:1 bmunicipality byear using mws.dta
	xtset bmunicipality
	xtreg mainoutcome pre6XMW pre5XMW pre4XMW pre3XMW pre2XMW post1XMW post2XMW post3XMW post4XMW post5XMW post4XMW i.byear i.byear#i.regionXurban if wrongregister==0, fe vce(cluster bmunicipality)
	outreg2 using `i', cttop(coefficient) word label ci level(95) e(all) dec(3)
}
*Copy the estimated event-study coefficients (called `i'point) and confidence intervals (called `i'lower `i'upper) to a new Stata dataset
*Note that event years >=6 and <=6 are esimated but not plotted:
label define eventyear 1 "-5th year" 2 "-4th year" 3 "-3rd year" 4 "-2nd year" 5 "-1st year" 6 "1st year" 7 "2nd year" ///
 8 "3rd year" 9 "4th year" 10 "5th year" 
label values eventyear eventyear

tsset eventyear
foreach i in hospitalshare earlymortality latemortality neonatalmortality {
	tsline `i'point `i'lower `i'upper
	graph export `i'eventstudy.eps, as(eps) replace
}

*Table 2
foreach i in lnlabourincome disabled unemployed {
	use `i'.dta, replace
	merge m:1 bmunicipality byear using mws.dta
	xtset bmunicipality
	xtreg mainoutcome postXMW i.byear i.byear#i.regionXurban i.sex if wrongregister==0, fe vce(cluster bmunicipality)
	outreg2 using `i', cttop(coefficient) word label se e(all) dec(3)
}

*Figure 3
foreach i in lnlabourincome disabled unemployed {
	use `i'.dta, replace
	merge m:1 bmunicipality byear using mws.dta
	xtset bmunicipality
	xtreg mainoutcome pre6XMW pre5XMW pre4XMW pre3XMW pre2XMW post1XMW post2XMW post3XMW post4XMW post5XMW post4XMW i.byear i.byear#i.regionXurban i.sex if wrongregister==0, fe vce(cluster bmunicipality)
	outreg2 using `i', cttop(coefficient) word label ci level(95) e(all) dec(3)
}
*Copy the estimated event-study coefficients (called `i'point) and confidence intervals (called `i'lower `i'upper) to a new Stata dataset
*Note that event years >=6 and <=6 are esimated but not plotted:
label define eventyear 1 "-5th year" 2 "-4th year" 3 "-3rd year" 4 "-2nd year" 5 "-1st year" 6 "1st year" 7 "2nd year" ///
 8 "3rd year" 9 "4th year" 10 "5th year" 
label values eventyear eventyear

tsset eventyear
foreach i in lnlabourincome disabled unemployed {
	tsline `i'point `i'lower `i'upper
	graph export `i'eventstudy.eps, as(eps) replace
}


*Table 3
foreach i in totalhospitalstay yearsofschooling secondaryschool specificfield service nonmanual {
	use `i'.dta, replace
	merge m:1 bmunicipality byear using mws.dta
	xtset bmunicipality
	xtreg mainoutcome postXMW i.byear i.byear#i.regionXurban i.sex if wrongregister==0, fe vce(cluster bmunicipality)
	outreg2 using `i', cttop(coefficient) word label se e(all) dec(3)
}

*Figure 4
foreach i in totalhospitalstay yearsofschooling secondaryschool specificfield service nonmanual {
	use `i'.dta, replace
	merge m:1 bmunicipality byear using mws.dta
	xtset bmunicipality
	xtreg mainoutcome pre6XMW pre5XMW pre4XMW pre3XMW pre2XMW post1XMW post2XMW post3XMW post4XMW post5XMW post4XMW i.byear i.byear#i.regionXurban i.sex if wrongregister==0, fe vce(cluster bmunicipality)
	outreg2 using `i', cttop(coefficient) word label ci level(95) e(all) dec(3)
}
*Copy the estimated event-study coefficients (called `i'point) and confidence intervals (called `i'lower `i'upper) to a new Stata dataset
*Note that event years >=6 and <=6 are esimated but not plotted:
label define eventyear 1 "-5th year" 2 "-4th year" 3 "-3rd year" 4 "-2nd year" 5 "-1st year" 6 "1st year" 7 "2nd year" ///
 8 "3rd year" 9 "4th year" 10 "5th year" 
label values eventyear eventyear

tsset eventyear
foreach i in totalhospitalstay yearsofschooling secondaryschool specificfield service nonmanual {
	tsline `i'point `i'lower `i'upper
	graph export `i'eventstudy.eps, as(eps) replace
}

*Table 4
*Sulpha
foreach i in neonatalmortality  {
	use `i'.dta, replace
	merge m:1 bmunicipality byear using mws.dta
	drop _merge
	merge m:1 bmunicipality byear using sulpha.dta
	xtset bmunicipality
	xtreg mainoutcome postXMW cointervention i.byear if wrongregister==0, fe vce(cluster bmunicipality)
	outreg2 using sulpha, cttop(coefficient) word label se e(all) dec(3)
	xtreg mainoutcome postXMW cointervention postXMW#cointervention i.byear if wrongregister==0, fe vce(cluster bmunicipality)
	outreg2 using sulpha, cttop(coefficient) word label se e(all) dec(3)
}
foreach i in  lnlabourincome {
	use `i'.dta, replace
	merge m:1 bmunicipality byear using mws.dta
	drop _merge
	merge m:1 bmunicipality byear using sulpha.dta
	xtset bmunicipality
	xtreg mainoutcome postXMW cointervention i.byear i.sex if wrongregister==0, fe vce(cluster bmunicipality)
	outreg2 using sulpha, cttop(coefficient) word label se e(all) dec(3)
	xtreg mainoutcome postXMW cointervention postXMW#cointervention i.byear i.sex if wrongregister==0, fe vce(cluster bmunicipality)
	outreg2 using sulpha, cttop(coefficient) word label se e(all) dec(3)
}
*Well-child program
foreach i in neonatalmortality  {
	use `i'.dta, replace
	merge m:1 bmunicipality byear using mws.dta
	drop _merge
	merge m:1 bmunicipality byear using sulpha.dta
	xtset bmunicipality
	xtreg mainoutcome postXMW cointervention i.byear i.byear#i.regionXurban if wrongregister==0, fe vce(cluster bmunicipality)
	outreg2 using sulpha, cttop(coefficient) word label se e(all) dec(3)
	xtreg mainoutcome postXMW cointervention postXMW#cointervention i.byear i.byear#i.regionXurban if wrongregister==0, fe vce(cluster bmunicipality)
	outreg2 using sulpha, cttop(coefficient) word label se e(all) dec(3)
}
foreach i in  lnlabourincome {
	use `i'.dta, replace
	merge m:1 bmunicipality byear using mws.dta
	drop _merge
	merge m:1 bmunicipality byear using wellchild.dta
	xtset bmunicipality
	xtreg mainoutcome postXMW cointervention i.byear i.byear#i.regionXurban i.sex if wrongregister==0, fe vce(cluster bmunicipality)
	outreg2 using wellchild, cttop(coefficient) word label se e(all) dec(3)
	xtreg mainoutcome postXMW cointervention postXMW#cointervention i.byear i.byear#i.regionXurban i.sex if wrongregister==0, fe vce(cluster bmunicipality)
	outreg2 using wellchild, cttop(coefficient) word label se e(all) dec(3)
}

*Figure 5
*A
	use lnlabourincome.dta, replace
	merge m:1 bmunicipality byear using mws.dta
	xtset bmunicipality
	gen byearXregionXurban=byear*regionXurban
	tab byearXregionXurban, gen(bybr)
    tab byear, gen(by)
	tab sex, gen(s)
	save tempcf, replace
	
	use tempcf, replace
	    xtrifreg  mainoutcome   postXMW bybr1-bybr784 by1-by16 s1 if wrongregister==0, quantile(0.1) w(0.06) fe i(bmunicipality) 
    matrix B=e(b)
    svmat double B, name(coef)
    gen quant=0.1
    keep quant coef1  
    keep if _n==1  
    save distrcoefs_mw, replace
  
forvalues q = 0.2(0.10)1.00 {
    use tempcf, replace
    xtrifreg  mainoutcome  postXMW bybr1-bybr784 by1-by16 s1 if wrongregister==0, quantile(`q') w(0.06) fe i(bmunicipality) 
    matrix B=e(b)
    svmat double B, name(coef)
    gen quant=`q'
    keep quant coef1  
    keep if _n==1  
    append using distrcoefs_mw
    save distrcoefs_mw, replace
    use tempcf, replace
 }
graph twoway (connected coef1 quant if quant>0.0 & quant<1.0,  lcolor(black) mcolor(black) mfcolor(white)  ) ///
   , xlabel(0.0 0.2 0.4 0.6 0.8 1.0) ytitle("Labour income estimate")  yline(0.051, lstyle(grid) lcolor(blue) lpattern(dash))  ylabel(0.0(0.04)0.25, nogrid) /// 
  yline(0, lstyle(grid) lcolor(black) lpattern(dot))  scheme(lean1) graphregion(color(white)) bgcolor(white) ///
  legend(on)
graph export distreffects5A.eps, replace	

*B
	use tempcf, replace
	xtrifreg  mainoutcome   postXTypeIMW postXTypeIIMW postXPrivateMW bybr1-bybr784 by1-by16 s1 if wrongregister==0, quantile(0.1) w(0.06) fe i(bmunicipality) 
    matrix B=e(b)
    svmat double B, name(coef)
    gen quant=0.1
    keep quant coef1 coef2 coef3 
    keep if _n==1  
    save distrcoefs_mw13, replace
  
forvalues q = 0.2(0.10)1.00 {
    use tempcf, replace
    xtrifreg  mainoutcome   postXTypeIMW postXTypeIIMW postXPrivateMW bybr1-bybr784 by1-by16 s1 if wrongregister==0, quantile(`q') w(0.06) fe i(bmunicipality) 
    matrix B=e(b)
    svmat double B, name(coef)
    gen quant=`q'
    keep quant coef1 coef2 coef3  
    keep if _n==1  
    append using distrcoefs_mw
    save distrcoefs_mw13, replace
    use tempcf, replace
 }
graph twoway (connected coef1 quant if quant>0.0 & quant<1.0,  lcolor(black) mcolor(black) mfcolor(white) msymbol(triangle)  ) ///
|| (connected coef2 quant if quant>0.0 & quant<1.0, lcolor(teal) mcolor(teal) mfcolor(white) msymbol(square) ) ///
|| (connected coef3 quant if quant>0.0 & quant<1.0, lcolor(purple) mcolor(purple) mfcolor(white) msymbol(x) ) ///
   , xlabel(0.0 0.2 0.4 0.6 0.8 1.0) ytitle("Labour income estimate")  yline(0.051, lstyle(grid) lcolor(blue) lpattern(dash))  ylabel(0.0(0.04)0.25, nogrid) /// 
  yline(0, lstyle(grid) lcolor(black) lpattern(dot))  scheme(lean1) graphregion(color(white)) bgcolor(white) ///
  legend(on)
graph export distreffects5B.eps, replace


***END****